<!--  -->
<template>
  <nav
    class="nav-box"
    :style="{'background':getSystemConfig.theme}"
    :class="{'close':getSystemConfig.isCollapse}"
    v-loading="getIsLoading"
  >
    <!-- 用户信息 -->
    <div class="user-detail">
      <!-- 用户头像 -->
      <div class="user-avatar">
        <img :src="getUserInfo.avatar" alt />
      </div>
      <div class="user-info">
        <el-dropdown trigger="click">
          <span class="el-dropdown-link role">
            {{getUserInfo.account}}
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <p class="account">( {{getUserInfo.role}} )</p>
          <el-dropdown-menu slot="dropdown">
            <router-link to="/dashboard/personal/profile">
              <el-dropdown-item>个人信息</el-dropdown-item>
            </router-link>
            <router-link to="/dashboard/personal/history">
              <el-dropdown-item>操作记录</el-dropdown-item>
            </router-link>
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <!-- 导航菜单 -->
    <el-menu
      class="sidebar"
      :backgroundColor="getSystemConfig.theme"
      :collapse="getSystemConfig.isCollapse"
      :router="true"
    >
      <el-menu-item index="1">
        <i class="el-icon-s-home"></i>
        <router-link to="/dashboard">
          <span class="slot">后台首页</span>
        </router-link>
        <!-- <router-link to="/dashboard" slot="title">后台首页</router-link> -->
      </el-menu-item>
      <el-submenu :index="v.path" v-for="(v,k) in getRoutes" :key="k">
        <template slot="title">
          <i :class="v.icon"></i>
          <span slot="title">{{v.title}}</span>
          <!-- <span v-if="v.hasOwnProperty('children')">{{v.name}}</span> -->
          <!-- <router-link v-if="!v.hasOwnProperty('children')" to="v.path">{{v.name}}</router-link> -->
        </template>
        <el-menu-item
          :index="'/dashboard/'+v.path+'/'+item.path"
          v-for="(item,index) in v.children"
          :key="index"
        >{{item.title}}</el-menu-item>
      </el-submenu>
    </el-menu>
  </nav>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
export default {
  methods: {
    ...mapActions('user', ['clearUserInfo']),
    ...mapActions('system', ['clearRoutes']),
    logout () {
      console.log('退出登录..')
      this.$http.logout().then(res => {
        // console.log(res);
        if (res && res.data.code === 200) {
          // 退出成功，服务器已经将当前的token销毁
          this.clearUserInfo().then(() => {
            this.$message({
              type: 'success',
              message: '您已成功退出登录！'
            })
            // 清空本地系统路由数据
            this.clearRoutes()
            // 重定向到登录页
            this.$router.push('/login')
            setTimeout(function () {
              window.location.reload()
            }, 100)
          })
        }
      })
    }
  },
  computed: {
    ...mapGetters('user', ['getUserInfo']),
    ...mapGetters('system', ['getSystemConfig', 'getRoutes', 'getIsLoading'])
  }
}
</script>

<style scoped lang="less">
.nav-box {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  overflow: auto;
  width: 220px;
  height: 100%;
  span {
    font-weight: bold;
  }
  .user-detail {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 25px;
    flex-direction: column;
    .user-avatar {
      width: 70px;
      height: 70px;
      img {
        width: 70px;
        height: 70px;
        border-radius: 50%;
        border: 2px solid #ccc;
      }
    }
    .user-info {
      margin-top: 10px;
      .el-dropdown {
        color: #dfe4ed;
        &:hover {
          cursor: pointer;
        }
      }
      .role {
        font-size: 15px;
        font-weight: 600;
      }
      .account {
        font-size: 13px;
      }
    }
  }
  .el-menu {
    border: 0;
    background: none;
    a {
      color: #a7b1c2;
    }

    .el-menu-item {
      color: #a7b1c2;
      height: 50px;
      line-height: 50px;
      &:hover {
        color: #fff;
        i {
          color: #fff;
        }
        a {
          color: #fff;
        }
      }
    }
    .el-submenu {
      /deep/.el-submenu__title {
        height: 50px;
        line-height: 50px;
        span {
          color: #a7b1c2;
        }
      }
      &:hover {
        span,
        i {
          color: #fff;
        }
      }
      .el-menu--inline {
        .el-menu-item {
          height: 46px;
          line-height: 46px;
          text-indent: 20px;
        }
      }
    }
  }
  i {
    color: #a7b1c2;
    margin-right: 5px;
    // font-size: 16px;
  }
}
.nav-box.close {
  width: 64px;
  .user-detail {
    position: relative;
    .user-avatar {
      width: 40px;
      height: 40px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        border: 0;
      }
    }
    .user-info {
      overflow: hidden;
      .el-dropdown {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        .el-dropdown-link {
          display: block;
          height: 60px;
        }
      }
      .role {
        font-size: 0px;
      }
      .account {
        font-size: 0px;
      }
    }
  }
  .el-menu {
    .el-menu-item,
    .el-submenu {
      i.fa {
        margin-left: 3px;
        font-size: 16px;
      }
    }
  }
  .fa-home,
  .fa-gear,
  .fa-shopping-cart {
    font-size: 18px;
  }
}

.el-dropdown-menu__item:focus,
.el-dropdown-menu__item:not(.is-disabled):hover {
  background-color: #f5f5f5;
  color: #262626;
}

.el-menu--vertical {
  .el-menu--popup {
    .el-menu-item {
      color: #a7b1c2;
      height: 46px;
      line-height: 46px;
      &:hover {
        color: #fff;
      }
    }
  }
}
</style>
